package com.ambimmort.shj.service.LslService;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Hfdw;
import cn.net.greenet.db.bean.ProCharacterLibrary;
import cn.net.greenet.generalfile.logic.GeneralFileProcess;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ma on 2015/7/30.
 */
@Service
public class RSUService {

//    @Autowired
//    private SessionFactory hibernateSessionFactory;

    ResultSet rs = null;
    BaseDao baseDao = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public String readWhite2UI(String pageCount, String page, String MessageType, String policy_instance
            , String iDisplayStart, String iDisplayLength){

        JSONObject json = new JSONObject();
        JSONArray result = new JSONArray();
        init();
        rs = baseDao.executeSQL("select *from Hfsw", new Object[]{});
        int length = 0;
        try {
            length = rs.getRow();
            List li = new ArrayList();
            if (rs.next()){
                li.add(rs);
            }
            json.put("iTotalRecords", length);
            json.put("iTotalDisplayRecords", length);
            for (Object li1 : li) {
                Hfdw hfdw = (Hfdw) li1;
                JSONArray one = new JSONArray();
                one.add(hfdw.getFilter());
                one.add("<a class='red' href='javascript:void(0)' title='删除' onclick='delectObj(" + hfdw.getId() + ")'><i class='icon-trash bigger-130'></i></a>");
                result.add(one);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        json.put("aaData", result);
        close();
        return json.toString();
    }
//    public String readWhite2UI(String pageCount, String page, String MessageType, String policy_instance
//            , String iDisplayStart, String iDisplayLength) {

//        JSONObject json = new JSONObject();
//        JSONArray result = new JSONArray();
//        Session session = hibernateSessionFactory.openSession();
//        session.clear();
//        Query query = session.createQuery("From Hfdw");
//        int length = query.list().size();
//        query.setFirstResult(Integer.parseInt(iDisplayStart));
//        query.setMaxResults(Integer.parseInt(iDisplayLength));
//        List li = query.list();
//        session.close();
//        json.put("iTotalRecords", length);
//        json.put("iTotalDisplayRecords", length);
//        for (Object li1 : li) {
//            Hfdw hfdw = (Hfdw) li1;
//            JSONArray one = new JSONArray();
//            one.add(hfdw.getFilter());
//            one.add("<a class='red' href='javascript:void(0)' title='删除' onclick='delectObj(" + hfdw.getId() + ")'><i class='icon-trash bigger-130'></i></a>");
//            result.add(one);
//        }
//        json.put("aaData", result);
//
//        return json.toString();
//    }

    public Boolean show_Del_Pro_Character_LibraryService(String ids) {

        boolean flag = false;
        String[] id = ids.split(",");
        try {
            /* TODO output your page here. You may use following sample code. */
            SessionFactory sf = HibernateUtil.getSessionFactory();
            Session session = sf.openSession();
            session.clear();
            for (int i = 0; i < id.length; i++) {
                String idd = id[i];
                Query query = session.createQuery("delete ProCharacterLibrary as pl where pl.id =?");
                query.setInteger(0, Integer.parseInt(idd));
                query.executeUpdate();
            }
            session.close();
            flag = true;
        } finally {
            return flag;
        }
    }

    public Boolean uplodProtocolBank2DB(String path, String MessageType, String policy_instance, GeneralFileProcess pro) {

        init();
        File src = new File(path);
        ProCharacterLibrary p = new ProCharacterLibrary();
        p.setName(src.getName());
        p.setPath(path);
        Date d = new Date();
        p.setUptime(new Date(d.getTime()));
        String sql = "insert into pro_character_library(name,path,uptime) values(?,?,?)";
        Object[] params={src.getName(),path,new Date(d.getTime())};
        baseDao.executeUpdate(sql, params);
        boolean result = true;
        close();
        return result;

//        File src = new File(path);
//        Session session = hibernateSessionFactory.openSession();
//        session.clear();
//        ProCharacterLibrary p = new ProCharacterLibrary();
//        p.setName(src.getName());
//        p.setPath(path);
//        Date d = new Date();
//        p.setUptime(new java.sql.Date(d.getTime()));
//        session.save(p);
//        session.close();
//        boolean result = true;
//        return result;
    }
}
